home *** CD-ROM | disk | FTP | other *** search
- package com.commerceone.util.io;
-
- import com.commerceone.util.contract.Contract;
- import java.io.IOException;
- import java.io.Reader;
-
- public class ReaderPair extends Reader {
- private Reader begin;
- private final Reader end;
-
- public void close() throws IOException {
- try {
- if (this.begin != null) {
- this.begin.close();
- }
- } finally {
- this.end.close();
- }
-
- }
-
- public ReaderPair(Reader car, Reader cdr) {
- Contract.require(car != null && cdr != null);
- this.begin = car;
- this.end = cdr;
- }
-
- public int read(char[] cbuf, int off, int len) throws IOException {
- int charsRead = 0;
- if (this.begin != null) {
- charsRead = this.begin.read(cbuf, off, len);
- }
-
- if (charsRead < 0) {
- this.finishBegin();
- charsRead = 0;
- }
-
- if (charsRead < len) {
- int result = this.end.read(cbuf, off + charsRead, len - charsRead);
- return result < 0 ? result : result + charsRead;
- } else {
- return charsRead;
- }
- }
-
- public int read() throws IOException {
- if (this.begin != null) {
- int charRead = this.begin.read();
- if (charRead > -1) {
- return charRead;
- }
-
- this.finishBegin();
- }
-
- return this.end.read();
- }
-
- private void finishBegin() {
- try {
- this.begin.close();
- } catch (IOException var2) {
- }
-
- this.begin = null;
- }
- }
-